from flask_apscheduler import APScheduler
import time
from model.models import db, TSysVideo
from apps import app, app_vedio_playbook

# 实例化 APScheduler
scheduler = APScheduler()

@scheduler.task('interval', id='job_cover_fill', hours=1)
def job_cover_fill():  # 运行的定时任务的函数
    print('定时任务' + str(time.time()))
    try:
        with app.app.app_context():
            tsVideo_list = TSysVideo.query.all()
            if tsVideo_list is None:
                return
            for video in tsVideo_list:
                try:
                    if video.user_id is None or video.cover_img_name is None or video.cover_img_name == '':
                        continue
                    if video.cover_img_base64 is None:
                        base64 = app_vedio_playbook.get_cover_image_base64(video.user_id, video.cover_img_name)
                        if base64 is None or base64 == '':
                            continue
                        video.cover_img_base64 = base64
                        db.session.commit()
                except Exception as e:
                    print('补足封面图片异常')
    except Exception as e:
        print('补足封面图片异常')